﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace HoaDon
{
    public partial class DM_TPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Mon"] == null)
                {
                    Response.Redirect("~\\DM_Mon.aspx");
                    return;
                }
                BindData();
                lbError.Text = "";
                lbMon.Text = Session["TenMon"].ToString();                
            }
        }

        protected DataTable getDataSourceDvt()
        {
            DataTable dt = new DataTable();
            dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmdvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND Status = '1'");
            return dt;
        }

        protected DataTable getDataSourceVt()
        {
            DataTable dt = new DataTable();
            dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmvt WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND NL_yn = 1 AND vtID != '"+Session["Mon"].ToString()+"' ");
            return dt;
        }

        private void BindData()
        {
            DataTable dt = new DataTable();
            if (Session["Mon"] != null)
                dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM vdmdl2 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND VtID = '" + Session["Mon"].ToString() + "'");

            gdvban.DataSource = dt;
            gdvban.DataBind();
            if (dt.Rows.Count == 0)
            {
                DataRow dtr = dt.NewRow();
                dt.Rows.Add(dtr);
                gdvban.DataSource = dt;
                gdvban.DataBind();
                gdvban.Rows[0].Enabled = false;
            }
        }

        protected void EditDM(object sender, GridViewEditEventArgs e)
        {
            gdvban.EditIndex = e.NewEditIndex;
            BindData();
            lbError.Text = "";
        }
        protected void CancelEditDM(object sender, GridViewCancelEditEventArgs e)
        {
            gdvban.EditIndex = -1;
            BindData();
            lbError.Text = "";
        }

        protected void OnPaging(object sender, GridViewPageEventArgs e)
        {
            BindData();
            gdvban.PageIndex = e.NewPageIndex;
            gdvban.DataBind();
            lbError.Text = "";
        }

        protected void UpdateDM(object sender, GridViewUpdateEventArgs e)
        {
            string id = ((Label)gdvban.Rows[e.RowIndex].FindControl("FK_vtID")).Text;
            string heso = ((TextBox)gdvban.Rows[e.RowIndex].FindControl("tbhe_so")).Text;

            if (heso == "")
            {
                lbError.Text = "Không để trống số lượng";
                return;
            }

            try
            {
                SqlEXEC.ExecCommand(Session["strConn"].ToString(), "UPDATE dmdl SET he_so = '"+heso+"', User_id2='" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', datetime2 = GETDATE() WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_VtID= '" + id + "' AND VtID = '"+Session["Mon"].ToString()+"'");
                gdvban.EditIndex = -1;
                BindData();
                lbError.Text = "";
            }
            catch (Exception ex)
            {
                lbError.Text = "Xảy ra lỗi khi cập nhật dữ liệu";
                gdvban.EditIndex = -1;
                BindData();
            }
        }

        protected void DeleteDM(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            try
            {                
                if (Session["Mon"] != null)
                    SqlEXEC.ExecCommand(Session["strConn"].ToString(), "DELETE FROM dmdl WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"].ToString() + "' AND FK_vtID= '" + bt.CommandArgument + "' AND vtID = '" + Session["Mon"].ToString() + "' ");
            }
            catch (Exception ex)
            {
                lbError.Text = "Đã phát sinh dữ liệu không thể xóa";
            }
            BindData();
        }

        protected void AddNewDM(object sender, EventArgs e)
        {
            try
            {
                string fk_vtid = ((DropDownList)gdvban.FooterRow.FindControl("drfvtid")).SelectedValue.ToString();
                string he_so = ((TextBox)gdvban.FooterRow.FindControl("txthe_so")).Text;
                if (fk_vtid == "")
                {
                    lbError.Text = "Không để trống mã";
                    return;
                }
                if (he_so == "")
                {
                    lbError.Text = "Không để trống số lượng";
                    return;
                }
                if (Session["Mon"] != null)
                {
                    if (SqlEXEC.ExecCommand(Session["strConn"].ToString(), "INSERT INTO dmdl (vtID,FK_VtID,FK_DatabaseID,he_so,user_id0,datetime0,user_id2,datetime2) VALUES ('" + Session["Mon"].ToString() + "','" + fk_vtid + "','" + Session["FK_DatabaseID"].ToString() + "','" + he_so + "','" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', GETDATE(),'" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "', GETDATE())") == -1)
                        lbError.Text = "Mã đã tồn tại, chọn mã khác";
                    else
                    {
                        BindData();
                        lbError.Text = "";
                    }
                }
            }
            catch (Exception ex)
            {
                lbError.Text = "Phải chọn nguyên liệu!";
                return;
            }

        }

    }
}
